/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：DancingScreen.java <br>
 * 创建时间：2014-2-27 下午7:23:56 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.expand.minigame;

import java.util.ArrayList;

import lostland.gmud.exv2.Assets;
import lostland.gmud.exv2.Direction;
import lostland.gmud.exv2.FontSize;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.GameConstants;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.blgframework.IPixmap;
import lostland.gmud.exv2.blgframework.impl.BLGGraphics;
import lostland.gmud.exv2.blgframework.impl.BLGPixmap;
import lostland.gmud.exv2.ui.NotificationScreen;
import lostland.gmud.exv2.ui.core.ButtonControlledScreen;
import lostland.gmud.exv2.ui.core.NewButton;

/**
 * 类名：DancingScreen <br>
 * 说明：跳舞毯
 *
 * @author 12548
 */
public class DancingScreen extends ButtonControlledScreen {


	final int ax[] = {60, 12, 108, 60};
	final int ay[] = {108, 60, 60, 12};


	String s = "";

	private class arrow {
		Direction dir;
		int x = 60;
		int y = 60;


		arrow(Direction dir) {
			this.dir = dir;
		}


		int dis() {
			return Math.abs(x - ax[dir.maincharTileY()]) + Math.abs(y - ay[dir.maincharTileY()]);
		}

		int dis0() {
			return Math.abs(x - 60) + Math.abs(y - 60);
		}

		void move_forword() {
			x += dir.dx();
			y += dir.dy();
		}


		void show() {
			BLGGraphics g = (BLGGraphics) getGame().getGraphics();
			IPixmap pm = null;

			switch (dir) {
				case DOWN:
					pm = Assets.bdown;
					break;
				case LEFT:
					pm = Assets.bleft;
					break;
				case RIGHT:
					pm = Assets.bright;
					break;
				case UP:
					pm = Assets.bup;
					break;
				default:
					break;

			}

			if (pm != null) {
				g.drawPixmap(pm, x, y);
			}

		}

	}


	ArrayList<arrow> arrows = new ArrayList<>();


	Direction d = Direction.DOWN;
	int step = 0;

	int score = 0;
	float time_interval = 1.5f;
	float ttime = 0;

	int missed = 0;


	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#onButtonDown(lostland.gmud.exv2.ui.core.NewButton)
	 */
	@Override
	protected void onButtonDown(NewButton b) {
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#onButtonClick(lostland.gmud.exv2.ui.core.NewButton)
	 */
	@Override
	public void onButtonClick(NewButton b) {


		Direction bd = null;
		switch (b) {
			case NB_BACK:
				getGame().popToRootScreen();
				return;
			case NB_DOWN:
				bd = Direction.DOWN;
				break;
			case NB_ENTER:
				break;
			case NB_LEFT:
				bd = Direction.LEFT;
				break;
			case NB_MENU:
				break;
			case NB_NULL:
				break;
			case NB_RIGHT:
				bd = Direction.RIGHT;
				break;
			case NB_UP:
				bd = Direction.UP;
				break;
			default:
				break;

		}

		if (bd != null) {
			d = bd;
			step = (step + 1) % 4;
			arrow pr = null;
			switch (b) {
				case NB_DOWN:
				case NB_LEFT:
				case NB_RIGHT:
				case NB_UP:

					for (arrow i : arrows) {
						if (i.dir == bd) {
							int d = i.dis();
							if (d <= 1) {
								s = "Prefect!";
								score += 50;
							} else if (d <= 3) {
								s = "Excellent!";
								score += 20;
							} else if (d <= 6) {
								s = "Good!";
								score += 8;
							} else if (d <= 12) {
								s = "Pass!";
								score += 3;
							} else if (d <= 24) {
								s = "Fail!";
								score += 1;
								missed += 1;
							} else {
								continue;
							}

							if (score > Game.getInstance().data.dancing_best) {
								Game.getInstance().data.dancing_best = score;
							}

							if (time_interval > 0.19f) {
								time_interval *= 0.9f;
							}

							pr = i;

							break;

						}
					}


					break;
				default:
					break;

			}


			if (pr != null) {
				arrows.remove(pr);
			}

		}

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#show()
	 */
	@Override
	protected void show() {
		BLGGraphics g = (BLGGraphics) getGame().getGraphics();

		g.clear(Gmud.getBgColor());

		g.drawLine(0, 0, GameConstants.FBWIDTH, 0, 0);
		g.drawLine(0, 0, 0, GameConstants.FBHEIGHT, 0);
		g.drawLine(GameConstants.FBWIDTH, 0, GameConstants.FBWIDTH, GameConstants.FBHEIGHT, 0);
		g.drawLine(0, GameConstants.FBHEIGHT, GameConstants.FBWIDTH, GameConstants.FBHEIGHT, 0);

		g.drawLine(156, 0, 156, 144, 0);
		g.drawLine(0, 144, 320, 144, 0);

		g.drawText("SCORE:" + score, 24, 160, FontSize.FT_16PX);
		g.drawText("BEST:" + Game.getInstance().data.dancing_best, 180, 160, FontSize.FT_16PX);

		g.drawPixmap(Assets.adown, ax[0], ay[0]);
		g.drawPixmap(Assets.aleft, ax[1], ay[1]);
		g.drawPixmap(Assets.aright, ax[2], ay[2]);
		g.drawPixmap(Assets.aup, ax[3], ay[3]);

		for (arrow i : arrows) {
			i.show();
		}

		final int MAINCHAR_WIDTH = 32;
		final int MAINCHAR_HEIGHT = 48;

		BLGPixmap pixmap = (BLGPixmap) Assets.getMainchar();
		if (Gmud.mc.sex > 0) {
			pixmap = (BLGPixmap) Assets.getGirl();
		}

		g.drawPixmap(pixmap, 204, 24, MAINCHAR_WIDTH * 2, MAINCHAR_HEIGHT * 2
				, (step % 4) * MAINCHAR_WIDTH, d.maincharTileY() * MAINCHAR_HEIGHT, MAINCHAR_WIDTH, MAINCHAR_HEIGHT);

		g.draw2Text(s, 0, 0, FontSize.FT_12PX);

	}


	public void gene() {
		arrows.add(new arrow(Direction.values()[Gmud.rand.nextInt(Direction.values().length)]));
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DialogScreen#update(float)
	 */
	@Override
	public void update(float deltaTime) {

		if (arrows.isEmpty()) {
			gene();
		}

		super.update(deltaTime);

		if (!arrows.isEmpty())
			if (arrows.get(arrows.size() - 1).dis0() > Math.max(5, 24 - score / 200)) {
				ttime += deltaTime;
				if (ttime > time_interval) {
					gene();
					ttime = 0;
				}
			}


		arrow pr = null;
		for (arrow i : arrows) {
			i.move_forword();
			if (i.dis0() > 72) {
				pr = i;
				missed += 5;
				s = "Miss!";
			}
		}

		if (pr != null) {
			arrows.remove(pr);
		}


		if (missed > 30) {
			cancel();
		}

	}


	void cancel() {
		if (Gmud.mc.skills[46] == 0 && score > 5000 && Game.getInstance().data.getSyuume() >= 2) {
			Gmud.mc.skills[46] = 1;
			getGame().popToRootScreen();
			new NotificationScreen("你在跳舞机获得了" + score + "分，你领悟了凌波微步！").pushToGame();
		} else {
			getGame().popToRootScreen();
			new NotificationScreen("你在跳舞机获得了" + score + "分。").pushToGame();
		}
	}


}
